// ts文件中直接使用 useRouter报错
import router from "@/router";
import store from "@/store";
import ACCESS_ENUM from "@/access/accessEnum";
import { off } from "@arco-design/web-vue/es/_utils/dom";
import AccessEnum from "@/access/accessEnum";
import checkAccess from "@/access/checkAccess";

// const router = useRouter();
// const store = useStore();
// 在到达之前
router.beforeEach((to, from, next) => {
  console.log(to, store.state.user.loginUser, "hehe123");
  // if (to.meta.access === ACCESS_ENUM.ADMIN) {
  //   if (store.state.user.loginUser.userRole !== ACCESS_ENUM.ADMIN) {
  //     next("/noAuth");
  //     return;
  //   }
  // }

  const needAccess = (to.meta?.access as string) ?? ACCESS_ENUM.NOT_LOGIN;
  const loginUser = store.state.user.loginUser;
  // 必须要登录的页面，，拦截 ,记录这个页面
  if (needAccess !== ACCESS_ENUM.NOT_LOGIN) {
    // 第一次登录没有userRole ，，后面会设置为 NotLogin
    if (
      !loginUser ||
      loginUser.userRole === ACCESS_ENUM.NOT_LOGIN ||
      !loginUser.userRole
    ) {
      next(`/user/login?redirect=${to.fullPath}`);
      return;
    } else {
      // 登录过的，校验权限
      if (!checkAccess(loginUser, needAccess)) {
        next("/noAuth");
        return;
      }
    }
  }

  next();
});
